//
//  CShareCommentMgr.m
//  WZEfengAndEtong
//
//  Created by wanzhao on 16/3/2.
//  Copyright © 2016年 wanzhao. All rights reserved.
//

#import "CShareCommentMgr.h"
#import "FMDB.h"
#import "EFengContent.h"
#import "CFolderMgr.h"

@implementation CShareCommentMgr


- (BOOL)insertWithShareComment:(EFengContent *)content
{
    if(content == nil)
    {
        return NO;
    } else {
        if (M_ISSTRNULL(content.shareId) || M_ISSTRNULL(content.commentId) || M_ISSTRNULL(content.sendUserId)) {
            return NO;
        }
    }
    
    
    __block BOOL ret = NO;
    
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        
        FMResultSet *result = nil;
        if ([content.commentType isEqualToString:@"praise"]) {
            result = [db executeQuery:@"select * from EFENGSHARECONTENT where sendUserId = ? AND shareId = ? AND isDel = 0 AND isLocal = 0 AND commentType = ?",M_STOREVALUE(content.sendUserId),M_STOREVALUE(content.shareId),@"praise"];
        } else {
            result = [db executeQuery:@"select * from EFENGSHARECONTENT where commentId = ?",M_STOREVALUE(content.commentId)];
        }

        if (result && [result next]) {
            ret = YES;
        }
        [result close];

        if (ret) {
            
            if ([content.commentType isEqualToString:@"praise"]) {
                 ret = [db executeUpdate:@"UPDATE EFENGSHARECONTENT SET commentId = ?, date = ? where shareId = ? AND sendUserId = ?", M_STOREVALUE(content.commentId), M_STOREVALUE(content.date),M_STOREVALUE(content.shareId),M_STOREVALUE(content.sendUserId)];
            } else {
                ret = [db executeUpdate:@"UPDATE EFENGSHARECONTENT SET shareId = ?, sendUserId = ? , receiveUserId = ?, sendUserName = ?, receiveUserName = ?, commentType = ?, content = ?, date = ?,yuLiuZiduan1 = ?,yuLiuZiduan2 = ?,yuLiuZiduan3 = ? where commentId = ?", M_STOREVALUE(content.shareId), M_STOREVALUE(content.sendUserId), M_STOREVALUE(content.receiveUserId), M_STOREVALUE(content.sendUserName), M_STOREVALUE(content.receiveUserName), M_STOREVALUE(content.commentType), M_STOREVALUE(content.content), M_STOREVALUE(content.date),M_STOREVALUE(content.yuLiuZiduan1),@(content.yuLiuZiduan2),M_STOREVALUE(content.yuLiuZiduan3),M_STOREVALUE(content.commentId)];
            }

        } else {
            ret = [db executeUpdate:@"INSERT INTO EFENGSHARECONTENT(commentId, shareId, sendUserId, receiveUserId, sendUserName, receiveUserName, commentType, content, date, isRead, isClick,isLocal,yuLiuZiduan1,yuLiuZiduan2,yuLiuZiduan3) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",M_STOREVALUE(content.commentId), M_STOREVALUE(content.shareId), M_STOREVALUE(content.sendUserId), M_STOREVALUE(content.receiveUserId), M_STOREVALUE(content.sendUserName), M_STOREVALUE(content.receiveUserName), M_STOREVALUE(content.commentType), M_STOREVALUE(content.content), M_STOREVALUE(content.date),content.isRead?@1:@0,content.isClick?@1:@0,content.isLocal?@1:@0,M_STOREVALUE(content.yuLiuZiduan1),@(content.yuLiuZiduan2),M_STOREVALUE(content.yuLiuZiduan3)];

        }
    }];
    return ret;

}
 //bug 8985 fixed by lixu
-(NSArray *)getUnsendCommentList
{
    __block NSMutableArray *arrayM = [NSMutableArray array];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = nil;
        result = [db executeQuery:@"select * from EFENGSHARECONTENT where yuLiuZiduan2 = 1 AND (commentType = ? OR commentType = ?)", @"comment",@"reply"];
        while (result && [result next]) {
            EFengContent *content = [[EFengContent alloc] init];
            content.CommentIndex = [result intForColumn:@"CommentIndex"];
            content.shareId = [result stringForColumn:@"shareId"];
            content.commentId = [result stringForColumn:@"commentId"];
            content.sendUserId = [result stringForColumn:@"sendUserId"];
            content.receiveUserId = [result stringForColumn:@"receiveUserId"];
            content.sendUserName = [result stringForColumn:@"sendUserName"];
            content.receiveUserName = [result stringForColumn:@"receiveUserName"];
            content.commentType = [result stringForColumn:@"commentType"];
            content.content = [result stringForColumn:@"content"];
            content.date = [result stringForColumn:@"date"];
            content.isRead = [result boolForColumn:@"isRead"];
            content.isClick = [result boolForColumn:@"isClick"];
            content.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            content.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            content.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];
            [arrayM addObject:content];
            content = nil;
        }
    }];
    return [arrayM copy];

}
- (BOOL)batchInsertWithCommentList:(NSArray *)commentList
{
    if(commentList == nil && [commentList count] <= 0)
    {
        return NO;
    }
    //     FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] companyCommonDatabasePath]];
    __block BOOL ret = NO;
    
    [[FMShareDataBase shareDataBase].shareQueue inTransaction:^(FMDatabase *db, BOOL *rollback) {
        for(EFengContent *content in commentList)
        {
            FMResultSet *result = nil;
            if ([content.commentType isEqualToString:@"praise"]) {
                result = [db executeQuery:@"select * from EFENGSHARECONTENT where sendUserId = ? AND shareId = ? AND isDel = 0 AND isLocal = 0 AND commentType = ?",M_STOREVALUE(content.sendUserId),M_STOREVALUE(content.shareId),@"praise"];
            } else {
                result = [db executeQuery:@"select * from EFENGSHARECONTENT where commentId = ?",M_STOREVALUE(content.commentId)];
            }
            
            ret = NO;
            if (result && [result next]) {
                ret = YES;
            }
            [result close];

            if (ret) {
                
            } else {
                if (!M_ISSTRNULL(content.commentId) && !M_ISSTRNULL(content.sendUserId) && !M_ISSTRNULL(content.shareId)) {
                    ret = [db executeUpdate:@"INSERT INTO EFENGSHARECONTENT(commentId, shareId, sendUserId, receiveUserId, sendUserName, receiveUserName, commentType, content, date, isRead, isClick,isLocal,yuLiuZiduan1,yuLiuZiduan2,yuLiuZiduan3) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",M_STOREVALUE(content.commentId), M_STOREVALUE(content.shareId), M_STOREVALUE(content.sendUserId), M_STOREVALUE(content.receiveUserId), M_STOREVALUE(content.sendUserName), M_STOREVALUE(content.receiveUserName), M_STOREVALUE(content.commentType), M_STOREVALUE(content.content), M_STOREVALUE(content.date),content.isRead?@1:@0,content.isClick?@1:@0,content.isLocal?@1:@0,M_STOREVALUE(content.yuLiuZiduan1),@(content.yuLiuZiduan2),M_STOREVALUE(content.yuLiuZiduan3)];
                    
                    if(!ret)
                    {
                        *rollback = YES;
                    }
                    
                }

            }
            
        }
        
    }];
    return ret;

}
- (BOOL)updateWithShare:(EFengContent *)content
{
    if (content == nil) {
        return NO;
    }
    
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
         ret = [db executeUpdate:@"UPDATE EFENGSHARECONTENT SET shareId = ?, sendUserId = ? , receiveUserId = ?, sendUserName = ?, receiveUserName = ?, commentType = ?, content = ?, date = ?, isRead = ?, isClick = ? ,yuLiuZiduan1 = ?,yuLiuZiduan2 = ?,yuLiuZiduan3 = ? where shareId = ?", M_STOREVALUE(content.shareId), M_STOREVALUE(content.sendUserId), M_STOREVALUE(content.receiveUserId), M_STOREVALUE(content.sendUserName), M_STOREVALUE(content.receiveUserName), M_STOREVALUE(content.commentType), M_STOREVALUE(content.content), M_STOREVALUE(content.date),content.isRead?@1:@0,content.isClick?@1:@0,M_STOREVALUE(content.yuLiuZiduan1),@(content.yuLiuZiduan2),M_STOREVALUE(content.yuLiuZiduan3),M_STOREVALUE(content.commentId)];
    }];
    return ret;
}

- (BOOL)updatePraiseWithComment:(EFengContent *)comment
{
    if (comment == nil) {
        return NO;
    }
    if (M_ISSTRNULL(comment.shareId) || M_ISSTRNULL(comment.sendUserId)) {
        return NO;
    }
    
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"UPDATE EFENGSHARECONTENT SET commentId = ?, date = ? where shareId = ? AND sendUserId = ?", M_STOREVALUE(comment.commentId), M_STOREVALUE(comment.date),M_STOREVALUE(comment.shareId),M_STOREVALUE(comment.sendUserId)];
    }];
    return ret;

}
- (BOOL)batchUpdateWithCommentList:(NSArray *)commentList
{
    if(commentList == nil && [commentList count] <= 0)
    {
        return NO;
    }
    //     FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] companyCommonDatabasePath]];
    __block BOOL ret = NO;
    
    [[FMShareDataBase shareDataBase].shareQueue inTransaction:^(FMDatabase *db, BOOL *rollback) {
        for(EFengContent *content in commentList)
        {
            if (!M_ISSTRNULL(content.commentId)) {
                 ret = [db executeUpdate:@"UPDATE EFENGSHARECONTENT SET shareId = ?, sendUserId = ? , receiveUserId = ?, sendUserName = ?, receiveUserName = ?, commentType = ?, content = ?, companyId = ?, date = ?, isRead = ?, isClick = ? ,yuLiuZiduan1 = ?,yuLiuZiduan2 = ?,yuLiuZiduan3 = ? where commentId = ?", M_STOREVALUE(content.shareId), M_STOREVALUE(content.sendUserId), M_STOREVALUE(content.receiveUserId), M_STOREVALUE(content.sendUserName), M_STOREVALUE(content.receiveUserName), M_STOREVALUE(content.commentType), M_STOREVALUE(content.content), M_STOREVALUE(content.date),content.isRead?@1:@0,content.isClick?@1:@0,M_STOREVALUE(content.yuLiuZiduan1),@(content.yuLiuZiduan2),M_STOREVALUE(content.yuLiuZiduan3),M_STOREVALUE(content.commentId)];
                if(!ret)
                {
                    *rollback = YES;
                }
                
            }
        }
        
    }];
    return ret;

}

- (BOOL)batchUpdateUnreadCommentToReadCommentWithCommentList:(NSArray *)commentList
{
    if(commentList == nil && [commentList count] <= 0)
    {
        return NO;
    }
    //     FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] companyCommonDatabasePath]];
    __block BOOL ret = NO;
    
    [[FMShareDataBase shareDataBase].shareQueue inTransaction:^(FMDatabase *db, BOOL *rollback) {
        for(EFengContent *content in commentList)
        {
            if (!M_ISSTRNULL(content.commentId)) {
                ret = [db executeUpdate:@"UPDATE EFENGSHARECONTENT SET isRead = 1 where commentId = ?",M_STOREVALUE(content.commentId)];
                if(!ret)
                {
                    *rollback = YES;
                }
                
            }
        }
        
    }];
    return ret;
    
}

- (NSInteger)getShareCommentAllUnReadCount
{

    NSString *userId = [[CFolderMgr shareInstance].accMgr getRecentlyLoginAccount].userId;
    __block NSInteger count = 0;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        
        count = [db intForQuery:@"select COUNT(*) from EFENGSHARECONTENT where isRead = 0 AND isDel = 0 AND isLocal = 0 AND (shareId in (select ShareId from EFENGSHARE where releaseUserId = ?) OR receiveUserId = ?)",userId,userId];
    }];
    return count;

}

- (BOOL)updateUnAttentionCommentUnReadToRead
{
    __block BOOL ret = NO;
    NSString *userId = [[CFolderMgr shareInstance].accMgr getRecentlyLoginAccount].userId;
    
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        //仿微信关注
        
//        ret = [db intForQuery:@"UPDATE EFENGSHARECONTENT SET isRead = 1 where isRead = 0 AND isDel = 0 AND isLocal = 0 AND (shareId NOT in (select ShareId from EFENGSHARE where releaseUserId = ?) AND shareId NOT in (select ShareId from EFENGSHARECONTENT where sendUserId = ?))",userId,userId];
        //img关注
         ret = [db intForQuery:@"UPDATE EFENGSHARECONTENT SET isRead = 1 where isRead = 0 AND isDel = 0 AND isLocal = 0 AND receiveUserId <> ? AND shareId NOT in (select ShareId from EFENGSHARE where releaseUserId = ?)",userId,userId];
    }];
    return ret;

}
- (BOOL)updateAllUnreadCommentToReadComment
{
    __block BOOL ret = NO;
    
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        
        ret = [db executeUpdate:@"UPDATE EFENGSHARECONTENT SET isRead = 1 where isRead = 0"];
    }];
    return ret;
    
}

- (NSInteger)getSharePraiseAllUnReadCount
{
    __block NSInteger count = 0;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        
        count = [db intForQuery:@"select COUNT(*) from EFENGSHARECONTENT where isRead = 0 AND isLocal = 0 AND isDel = 0 AND commentType = ?",@"praise"];
    }];
    return count;

}

- (NSInteger)getSharePraiseAllReadCountWithShareId:(NSString *)shareId
{
    __block NSInteger count = 0;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        
        count = [db intForQuery:@"select COUNT(*) from EFENGSHARECONTENT where shareId = ? AND isLocal = 0 AND commentType = ? AND isDel = 0",shareId,@"praise"];
    }];
    return count;
    
}

- (NSArray *)getShareCommentWithShareId:(NSString *)shareId
{
    __block NSMutableArray *arrayM = [NSMutableArray array];

    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:@"select * from EFENGSHARECONTENT where shareId = ? AND isDel = 0 AND isLocal = 0 AND (commentType = ? OR commentType = ?) order by date  ASC",shareId,@"reply",@"comment"];
        while (result && [result next]) {
            EFengContent *content = [[EFengContent alloc] init];
            content.CommentIndex = [result intForColumn:@"CommentIndex"];
            content.shareId = [result stringForColumn:@"shareId"];
             content.commentId = [result stringForColumn:@"commentId"];
            content.sendUserId = [result stringForColumn:@"sendUserId"];
            content.receiveUserId = [result stringForColumn:@"receiveUserId"];
            content.sendUserName = [result stringForColumn:@"sendUserName"];
            content.receiveUserName = [result stringForColumn:@"receiveUserName"];
            content.commentType = [result stringForColumn:@"commentType"];
            content.content = [result stringForColumn:@"content"];
            content.date = [result stringForColumn:@"date"];
            content.isRead = [result boolForColumn:@"isRead"];
            content.isClick = [result boolForColumn:@"isClick"];
            content.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            content.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            content.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];
            [arrayM addObject:content];
            content = nil;
        }
        [result close];
    }];
    return [arrayM sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"date" ascending:YES]]];

}

- (NSArray *)getSharePraiseWithShareId:(NSString *)shareId
{
    __block NSMutableArray *arrayM = [NSMutableArray array];
    
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:@"select * from EFENGSHARECONTENT where shareId = ? AND isDel = 0 AND isLocal = 0 AND commentType = ? order by date DESC",shareId,@"praise"];
        while (result && [result next]) {
            EFengContent *content = [[EFengContent alloc] init];
            content.CommentIndex = [result intForColumn:@"CommentIndex"];
            content.shareId = [result stringForColumn:@"shareId"];
            content.commentId = [result stringForColumn:@"commentId"];
            content.sendUserId = [result stringForColumn:@"sendUserId"];
            content.receiveUserId = [result stringForColumn:@"receiveUserId"];
            content.sendUserName = [result stringForColumn:@"sendUserName"];
            content.receiveUserName = [result stringForColumn:@"receiveUserName"];
            content.commentType = [result stringForColumn:@"commentType"];
            content.content = [result stringForColumn:@"content"];
            content.date = [result stringForColumn:@"date"];
            content.isRead = [result boolForColumn:@"isRead"];
            content.isClick = [result boolForColumn:@"isClick"];
            content.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            content.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            content.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];
            [arrayM addObject:content];
            content = nil;
        }
        [result close];
    }];
    return [arrayM sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"date" ascending:YES]]];
    
}


- (NSArray *)getAllUnreadShareComment
{
    __block NSMutableArray *arrayM = [NSMutableArray array];
    NSString *userId = [[CFolderMgr shareInstance].accMgr getRecentlyLoginAccount].userId;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:@"select * from EFENGSHARECONTENT where isRead = 0 AND isDel = 0 AND isLocal = 0 AND (shareId in (select ShareId from EFENGSHARE where releaseUserId = ?) OR receiveUserId = ?) order by date  DESC",userId,userId];
        while (result && [result next]) {
            EFengContent *content = [[EFengContent alloc] init];
            content.CommentIndex = [result intForColumn:@"CommentIndex"];
            content.shareId = [result stringForColumn:@"shareId"];
            content.commentId = [result stringForColumn:@"commentId"];
            content.sendUserId = [result stringForColumn:@"sendUserId"];
            content.receiveUserId = [result stringForColumn:@"receiveUserId"];
            content.sendUserName = [result stringForColumn:@"sendUserName"];
            content.receiveUserName = [result stringForColumn:@"receiveUserName"];
            content.commentType = [result stringForColumn:@"commentType"];
            content.content = [result stringForColumn:@"content"];
            content.date = [result stringForColumn:@"date"];
            content.isRead = [result boolForColumn:@"isRead"];
            content.isClick = [result boolForColumn:@"isClick"];
            content.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            content.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            content.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];
            [arrayM addObject:content];
            content = nil;
        }
        [result close];
    }];
    return [arrayM sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"date" ascending:NO]]];

}
- (BOOL)delShareCommentWithShareId:(NSString *)shareId
{
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"delete from EFENGSHARECONTENT where shareId = ?",shareId];
    }];
    return ret;
}
- (BOOL)delShareCommentWithCommentId:(NSString *)CommentId
{
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        
        ret = [db executeUpdate:@"delete from EFENGSHARECONTENT where commentId = ?",CommentId];
    }];
    return ret;

}

- (BOOL)delSharePraiseWithShareId:(NSString *)shareId userId:(NSString *)userId
{
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"delete from EFENGSHARECONTENT where shareId = ? AND sendUserId = ? AND commentType = ?",shareId,userId,@"praise"];
    }];
    return ret;

}

- (EFengContent *)getZanIdWithShareId:(NSString *)shareId userId:(NSString *)userId
{
    __block EFengContent *comment = nil;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:@"select * from EFENGSHARECONTENT where shareId = ? AND isDel = 0 AND sendUserId = ? AND isLocal = 0 AND commentType = ?",shareId,userId,@"praise"];
        while (result && [result next]) {
            comment = [[EFengContent alloc] init];
            comment.CommentIndex = [result intForColumn:@"CommentIndex"];
            comment.date = [result stringForColumn:@"date"];
            comment.commentId = [result stringForColumn:@"commentId"];
            
        }
        [result close];
    }];
    return comment;
}

- (BOOL)isHasZanIdWithShareId:(NSString *)shareId userId:(NSString *)userId
{
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:@"select * from EFENGSHARECONTENT where shareId = ? AND isDel = 0 AND sendUserId = ? AND commentType = ? AND isLocal = 0",shareId,userId,@"praise"];
        
        while (result && [result next]) {
            ret = YES;
        }
    }];
    return ret;
}

- (BOOL)updateShareCommentIsUnNetCompletedWithComment:(EFengContent *)comment isDel:(BOOL)isDel
{
    if (comment == nil) {
        return NO;
    }
    
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"UPDATE EFENGSHARECONTENT SET yuLiuZiduan2 = 1, isDel = ? where commentId = ?",isDel?@1:@0,comment.commentId];
       
    }];
    return ret;

}

- (BOOL)updateCommentWithCommentId:(NSString *)commentId date:(NSString *)date
{
    __block BOOL ret = NO;

    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"UPDATE EFENGSHARECONTENT SET date = ?, yuLiuZiduan2 = 0 where commentId = ?",M_STOREVALUE(date),M_STOREVALUE(commentId)];
    }];
    return ret;

}
- (EFengContent *)getShareCommentWithCommentIndex:(NSInteger)CommentIndex
{
    __block EFengContent *content = nil;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:@"select * from EFENGSHARECONTENT where CommentIndex = ?",@(CommentIndex)];
        while (result && [result next]) {
            content = [[EFengContent alloc] init];
            content.CommentIndex = [result intForColumn:@"CommentIndex"];
            content.shareId = [result stringForColumn:@"shareId"];
            content.commentId = [result stringForColumn:@"commentId"];
            content.sendUserId = [result stringForColumn:@"sendUserId"];
            content.receiveUserId = [result stringForColumn:@"receiveUserId"];
            content.sendUserName = [result stringForColumn:@"sendUserName"];
            content.receiveUserName = [result stringForColumn:@"receiveUserName"];
            content.commentType = [result stringForColumn:@"commentType"];
            content.content = [result stringForColumn:@"content"];
            content.date = [result stringForColumn:@"date"];
            content.isRead = [result boolForColumn:@"isRead"];
            content.isClick = [result boolForColumn:@"isClick"];
            content.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            content.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            content.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];
        }
        [result close];
    }];
    return content;

}
- (BOOL)delShareCommentWithCommentIndex:(NSInteger)CommentIndex
{
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"delete from EFENGSHARECONTENT where CommentIndex = ?",@(CommentIndex)];
    }];
    return ret;

}
- (NSArray *)getUnSendShareComment
{
    __block NSMutableArray *arrayM = [NSMutableArray array];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:@"select * from EFENGSHARECONTENT where commentId = ? AND isLocal = 0",@""];
        while (result && [result next]) {
            EFengContent *content = [[EFengContent alloc] init];
            content.CommentIndex = [result intForColumn:@"CommentIndex"];
            content.shareId = [result stringForColumn:@"shareId"];
            content.commentId = [result stringForColumn:@"commentId"];
            content.sendUserId = [result stringForColumn:@"sendUserId"];
            content.receiveUserId = [result stringForColumn:@"receiveUserId"];
            content.sendUserName = [result stringForColumn:@"sendUserName"];
            content.receiveUserName = [result stringForColumn:@"receiveUserName"];
            content.commentType = [result stringForColumn:@"commentType"];
            content.content = [result stringForColumn:@"content"];
            content.date = [result stringForColumn:@"date"];
            content.isRead = [result boolForColumn:@"isRead"];
            content.isClick = [result boolForColumn:@"isClick"];
            content.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            content.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            content.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];
            [arrayM addObject:content];
            content = nil;
        }
        [result close];
    }];
    return arrayM;
 
}

- (EFengContent *)getMaxShareComment
{
    __block EFengContent *content = nil;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:@"select * from EFENGSHARECONTENT where isLocal = 0 AND CommentIndex = (select max(CommentIndex) from EFENGSHARECONTENT)"];
        while (result && [result next]) {
            content = [[EFengContent alloc] init];
            content.CommentIndex = [result intForColumn:@"CommentIndex"];
            content.shareId = [result stringForColumn:@"shareId"];
            content.commentId = [result stringForColumn:@"commentId"];
            content.sendUserId = [result stringForColumn:@"sendUserId"];
            content.receiveUserId = [result stringForColumn:@"receiveUserId"];
            content.sendUserName = [result stringForColumn:@"sendUserName"];
            content.receiveUserName = [result stringForColumn:@"receiveUserName"];
            content.commentType = [result stringForColumn:@"commentType"];
            content.content = [result stringForColumn:@"content"];
            content.date = [result stringForColumn:@"date"];
            content.isRead = [result boolForColumn:@"isRead"];
            content.isClick = [result boolForColumn:@"isClick"];
            content.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            content.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            content.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];
        }
        [result close];
    }];
    return content;

}

- (EFengContent *)getMaxUnreadShareComment
{
    __block EFengContent *content = nil;
    
    NSString *userId = [[CFolderMgr shareInstance].accMgr getRecentlyLoginAccount].userId;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:@"select * from EFENGSHARECONTENT where date = (select max(date) from EFENGSHARECONTENT where isLocal = 0 And isRead = 0 AND (shareId in (select ShareId from EFENGSHARE where releaseUserId = ?) OR receiveUserId = ?))",userId,userId];
        while (result && [result next]) {
            content = [[EFengContent alloc] init];
            content.CommentIndex = [result intForColumn:@"CommentIndex"];
            content.shareId = [result stringForColumn:@"shareId"];
            content.commentId = [result stringForColumn:@"commentId"];
            content.sendUserId = [result stringForColumn:@"sendUserId"];
            content.receiveUserId = [result stringForColumn:@"receiveUserId"];
            content.sendUserName = [result stringForColumn:@"sendUserName"];
            content.receiveUserName = [result stringForColumn:@"receiveUserName"];
            content.commentType = [result stringForColumn:@"commentType"];
            content.content = [result stringForColumn:@"content"];
            content.date = [result stringForColumn:@"date"];
            content.isRead = [result boolForColumn:@"isRead"];
            content.isClick = [result boolForColumn:@"isClick"];
            content.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            content.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            content.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];
        }
        [result close];
    }];
    return content;
    
}

- (NSString *)getMaxDateShareComment
{
    __block NSString *result = nil;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        result = [db stringForQuery:@"select max(date) from EFENGSHARECONTENT"];
    }];
    return result;

}

- (NSString *)getMinDateShareComment
{
    __block NSString *result = nil;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        result = [db stringForQuery:@"select min(date) from EFENGSHARECONTENT"];
    }];
    return result;

}
@end
